/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GoogleChartFactory;

import java.awt.Color;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Admin
 */
public class GoogleChartFactory {

    private static final String baseURL = "http://chart.googleapis.com/chart?chbh=a";
    
    private GoogleChartFactory() {
    }
       
    static private String createColor(Color base, int count)
    {
        StringBuilder url = new StringBuilder();
        ColorGenerator colorgen = new ColorGenerator(count,base);
        Iterator<Color> iterator = colorgen.iterator();
        url.append("&chco=");
        while(iterator.hasNext())
        {
            url.append(Integer.toHexString(iterator.next().getRGB()).substring(2, 8));
            url.append(",");
        }
        url.setLength(url.length() - 1);
        return url.toString();
    }
    
    static public String createPieChart(Map<String,Number> values, Integer height, Integer width, ChartTypes.PieCharts type)
    {
        StringBuilder url = new StringBuilder(baseURL);
        url.append("&cht=")
                .append(type.getIndex())
                .append("&chs=")
                .append(width.toString())
                .append("x")
                .append(height.toString());
                
        StringBuilder strLables = new StringBuilder("&chdl=");
        StringBuilder strValues = new StringBuilder("&chd=t:");
        
        for(Map.Entry<String,Number> i : values.entrySet())
        {
            strLables.append(i.getKey().replace(' ', '+')).append('|');
            strValues.append(i.getValue().toString()).append(',');
        }
        
        url.append(createColor(Color.BLUE,values.size()));
        
        strLables.setLength(strLables.length() - 1);
        strValues.setLength(strValues.length() - 1);
        url.append(strValues).append(strLables);
        return url.toString();
    }
    
    static public String createBarChart(Map<String,List<Number>> values, Integer height, Integer width, ChartTypes.BarCharts type)
    {
        // color & max
        
        StringBuilder url = new StringBuilder(baseURL);
        url.append("&cht=")
                .append(type.getIndex())
                .append("&chs=")
                .append(width.toString())
                .append("x")
                .append(height.toString());
             
        int min = 0;
        int max = 0;
                
        
        StringBuilder strLables = new StringBuilder("&chdl=");
        StringBuilder strValues = new StringBuilder("&chd=t:");
        
        for(Map.Entry<String,List<Number>> i : values.entrySet())
        {
            strLables.append(i.getKey().replace(' ', '+')).append('|');
            Iterator<Number> iterator = i.getValue().iterator();
            while (iterator.hasNext())
            {
                Number n = iterator.next();
                strValues.append(n.toString());
                if(iterator.hasNext()) strValues.append(',');
                if(min > n.intValue()) min = n.intValue();
                if(max < n.intValue()) max = n.intValue();
            }
            strValues.append('|');
        }
        strLables.setLength(strLables.length() - 1);
        strValues.setLength(strValues.length() - 1);
        
        url.append(createColor(Color.BLUE,values.size()));
        
        url.append("&chds=");
        for(int i = 0; i < values.size();i++)
        {
            if(i!=0) url.append(",");
            url.append(min).append(",").append(max);
        }
        
        url.append("&chxr=0,").append(min).append(",").append(max);
        
        url.append(strValues).append(strLables);
        return url.toString();
    }
    
    static public String createLineChart(Map<String,List<Number>> values, Integer height, Integer width, ChartTypes.SimpleLineCharts type)
    {
        ChartTypes.BarCharts barType = new ChartTypes.BarCharts("custom", type.getIndex());
        return createBarChart(values, height, width, barType);
    }
    
    static public String createXYLineChart(Map<String,Map<Number,Number>> values, Integer height, Integer width, ChartTypes.LineCharts type)
    {
        StringBuilder url = new StringBuilder(baseURL);
        url.append("&cht=")
                .append(type.getIndex())
                .append("&chs=")
                .append(width.toString())
                .append("x")
                .append(height.toString());
    
        int minX = 100;
        int minY = 100;
        int maxX = 0;
        int maxY = 0;
                
        StringBuilder strLables = new StringBuilder("&chdl=");
        StringBuilder strValues = new StringBuilder("&chd=t:");
        
        for(Map.Entry<String,Map<Number,Number>> i : values.entrySet())
        {
            strLables.append(i.getKey().replace(' ', '+')).append('|');
            StringBuilder valuesX = new StringBuilder();
            StringBuilder valuesY = new StringBuilder();
            for(Map.Entry<Number,Number> n : i.getValue().entrySet())
            {
                valuesX.append(n.getKey()).append(',');
                valuesY.append(n.getValue()).append(',');
                if(minY > n.getValue().intValue()) minY = n.getValue().intValue();
                if(maxY < n.getValue().intValue()) maxY = n.getValue().intValue();
                if(minX > n.getKey().intValue()) minX = n.getKey().intValue();
                if(maxX < n.getKey().intValue()) maxX = n.getKey().intValue();
            }
            valuesX.setLength(valuesX.length() - 1);
            valuesY.setLength(valuesY.length() - 1);
            strValues.append(valuesX).append('|').append(valuesY).append('|');
        }
        strLables.setLength(strLables.length() - 1);
        strValues.setLength(strValues.length() - 1);
        
        url.append(createColor(Color.BLUE,values.size()));
        
        url.append("&chds=");
        for(int i = 0; i < values.size();i++)
        {
            if(i!=0) url.append(",");
            url.append(minX).append(",").append(maxX).append(',').append(minY).append(",").append(maxY);
        }
        
        url.append("&chxr=0,").append(minX).append(",").append(maxX) .append("|1,").append(minY).append(",").append(maxY);
        
        url.append(strValues).append(strLables);
        return url.toString();
    }
}
